package com.basksoft.server.demo.datasource; 

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.springframework.core.env.Environment;

import com.basksoft.core.definition.datasource.DatasourceDefinition;
import com.basksoft.core.model.datasource.BuildinDatasourceProvider;
import com.basksoft.server.demo.ToolSpring;

/**
 * @author William.Jiang
 * @since 2021年04月11日
 */

public class SampleCenterDatasource implements BuildinDatasourceProvider {
	private static Properties properties = new Properties();
	private static DataSource dataSource = null;
	
	@Override
	public DataSource buildDatasource(DatasourceDefinition def) {
		if (dataSource!=null) return dataSource;
		
		Environment env = ToolSpring.getBean(Environment.class);
		// 加载DBCP配置文件
		DataSource dataSource = null;
		properties.put("username", env.getProperty("baskserver.sampledb.username"));
		properties.put("password", env.getProperty("baskserver.sampledb.password"));
		properties.put("driverClassName", env.getProperty("baskserver.sampledb.driverClassName"));
		properties.put("url",env.getProperty("baskserver.sampledb.url"));
		properties.put("validationQuery",env.getProperty("baskserver.sampledb.validationQuery"));

		try {
			dataSource = BasicDataSourceFactory.createDataSource(properties);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return dataSource;
	}
}
